define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Myproductdetail, $stateParams, GoDialog, Auth) {
		$scope.isHide = true; //默认隐藏
		$scope.input = new Object();
		$scope.input.price = '';
		$scope.detail_id = $stateParams.detail_id;
		$scope.region = $stateParams.region;
		$scope.store_name = $stateParams.store_name;
		$scope.timeOut = false;
		$scope.init_data = function() {
			$scope.check = Auth.check_user_type();
			$scope.store_id = Auth.get_store_id();
			//获取求购详细
			Myproductdetail.goods_detail($scope.detail_id, function(data,nowTime) {
				$scope.detailData = data;
				var time = Date.parse(data.end_time)/1000;
				if(nowTime > time){
					$scope.timeOut = true;
				}
				for(var i =0;i<$scope.detailData.goods.length;i++){
					if($scope.detailData.goods[i].brand_need==false){
						$scope.detailData.goods[i].brand_name = '不限';
					}
				}
			});
			Myproductdetail.store_info(function(data) {
				$scope.region = data.store.company_area.split(':');
				$scope.region = $scope.region[1];
				$scope.store_name = data.store.store_name;
			})
		}
		$scope.init_data();
		$scope.hide_apply = function(index) { //隐藏弹窗
			if(index == 1) { //保存数据
				if($scope.input.price) {
					if($scope.input.price<0){
						GoDialog.toast('请输入正确报价！');
						return;
					}
					Myproductdetail.offer($scope.detail_id, $scope.store_id, $scope.input.price, function(data) {
						if(data.success) {
							$scope.isHide = true;
							$scope.init_data();
							GoDialog.toast('报价成功！');
						} else {
							GoDialog.toast(data.msg);
						}
					});
				} else {
					GoDialog.toast('请输入本次报价');
				}
			} else {
				$scope.isHide = true;
			}
		}

		//显示弹窗
		$scope.show_apply = function() {
			if($scope.detailData.has_bidden == true) {
				GoDialog.toast('您已经报过价了！');
			} else
				$scope.isHide = false;
		}

	}

	controller.$inject = ['$scope', 'Myproductdetail', '$stateParams', 'GoDialog', 'Auth'];

	return app.registerController("myproductdetailctrl",controller);
});